---
name: useBoolean
route: /useBoolean
menu: 'State'
edit: false
sidebar: true
---
import JackBox from 'jackbox';

import Demo1 from './demo/demo1';
import Demo1CodeTsx from '!raw-loader!./demo/demo1.tsx';
import Demo1CodeJsx from '!raw-loader!./demo/demo1.jsx';

# useBoolean

A hook that elegantly manages boolean values.

## Examples

### Default usage

<JackBox jsCode={Demo1CodeJsx} tsCode={Demo1CodeTsx} demoName='Default usage' description='Default as a switch function,or accept a parameter to change state'>
  <Demo1 />
</JackBox>

## API

```javascript
const {
  state, 
  toggle,
  setTrue,
  setFalse
} = useBoolean(
  defaultValue?: boolean,
);
```

### Result

| Property | Description                                         | Type                 |
|----------|--------------------------------------|----------------------|
| state  | State value                         | boolean              |
| toggle | Trigger state change, accept an optional parameter | (value?: any) => void |
| setTrue | Set state value true | () => void |
| setFalse | Set state value false | () => void |

### Params

| Property | Description                                 | Type                   | Default |
|---------|----------------------------------------------|------------------------|--------|
| defaultValue | Optional，set a default value  | boolean \| undefined | false      |